<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Request;
use think\Config;

class Commitment extends AdminBase {
  protected $model;

  public function __construct(Request $request = NULL) {
    parent::__construct($request);
    $this->model = new \app\admin\model\Commitment();
  }

  public function _initialize() {
    parent::_initialize();
  }

  public function index() {
    $rows = $this->model
      ->alias('c')
      ->join('__BASE_USER__ u', 'u.id=c.creator_uid', 'left')
      ->field('c.*,u.realname')
      ->paginate();

    return view('', ['rows' => $rows]);
  }

  public function add() {
    return self::edit();
  }

  public function edit() {
    $id = input('id/d', 0, 'abs');
    if (!request()->isPost()) {
      $row = ($id > 0) ? $this->model->find($id) : [];

      return view('edit', ['row' => $row]);
    }
    $form = input('form/a', NULL);
    $result = $this->validate($form, 'Commitment');
    if (TRUE !== $result) {
      $this->error($result);
    }
    cmf_log('新增/编辑承诺书', $id, $form);

    $this->model->editJump($form, $id, url('index'));

  }

  public function delete() {
    $id = input('id/d', 0, 'abs');
    $r = $this->model->destroy($id);
    if (!$r) $this->error(lang('error') . $this->model->getError());

    cmf_log('删除承诺书', $id);

    $this->success(lang('success'));
  }


}